# required cmake version
cmake_minimum_required(VERSION 3.10)

# plugin目录下的代码由nvcc进行编译，编译完成后会被连接到上层目录
project(plugin)

# 查找CUDA环境，因为下面我们已经显式指定TensorRT和CUDA的安装环境路径，所以这里不要这一行也可以
set(CUDA_ARCHITECTURES 75 89)
cmake_policy(SET CMP0146 OLD)
find_package(CUDA REQUIRED)

enable_language(CUDA)
include_directories("${CUDA_INCLUDE_DIRS}")

# 声明一个路径，编译链接时会用到
set(TRT_INC     "/usr/include")
set(TRT_LIB     "/usr/lib/x86_64-linux-gnu")
set(CUDA_HOME   "/usr/local/cuda")
set(CUDNN_LIB   "/usr/lib/x86_64-linux-gnu")
set(CUDA_LIB    ${CUDA_HOME}/targets/x86_64-linux/lib)

#include_directories ("${PROJECT_SOURCE_DIR}")

set(CUDA_NVCC_FLAGS -w -gencode arch=compute_89,code=sm_89; -G)           # 设置NVCC的编译参数,-w表示屏蔽编译警告
file(GLOB_RECURSE CURRENT_HEADERS  *.h *.hpp *.cuh)                       # 包含所有的以指定后缀的文件进行编译
file(GLOB CURRENT_SOURCES  *.cpp *.cu)

source_group("Include" FILES ${CURRENT_HEADERS})                          # 添加头文件和源文件
source_group("Source"  FILES ${CURRENT_SOURCES}) 

cuda_add_library(plugin SHARED ${CURRENT_HEADERS} ${CURRENT_SOURCES})     # 生成动态库
set_property(TARGET plugin PROPERTY CUDA_ARCHITECTURES ${CUDA_ARCHITECTURES})
# cuda_add_library(gpu STATIC ${CURRENT_HEADERS} ${CURRENT_SOURCES})      # 生成静态库

# tensorrt libs
# target_include_directories(plugin PUBLIC ${TRT_HOME})                   # 目录需要设置PUBLIC
target_include_directories(plugin PUBLIC ${TRT_INC})
target_link_libraries(plugin ${TRT_LIB}/libnvparsers.so)
target_link_libraries(plugin ${TRT_LIB}/libnvinfer.so)
target_link_libraries(plugin ${TRT_LIB}/libnvinfer_plugin.so)

target_link_libraries(plugin -ldl -lpthread -lrt)
target_link_libraries(plugin /home/yangxianpku/.anaconda3/lib/libstdc++.so)

# cudnn libs
target_link_libraries(plugin ${CUDNN_LIB}/libcudnn.so)

# cuda  includes
target_include_directories(plugin PUBLIC ${CUDA_HOME})
target_include_directories(plugin PUBLIC ${CUDA_HOME}/include)
# cuda  libs
target_link_libraries(plugin ${CUDA_LIB}/libcublas.so)
target_link_libraries(plugin ${CUDA_LIB}/libculibos.a)                     # 这是一个静态连接库
target_link_libraries(plugin ${CUDA_LIB}/libcudart.so)